package com.fnzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fnzx.dao.ProjectDao;
import com.fnzx.entity.ProjectEntity;
import com.fnzx.entity.ProjectUserRelationEntity;
import com.fnzx.entity.dto.ProjectDto;
import com.fnzx.entity.vo.PersonalProjectVo;
import com.fnzx.entity.vo.ProjectAdminVo;
import com.fnzx.entity.vo.ProjectScoreAdminVo;
import com.fnzx.entity.vo.ProjectVo;
import com.fnzx.service.ProjectService;
import com.fnzx.service.ProjectUserRelationService;
import com.fnzx.service.ScoreService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("projectService")
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectEntity> implements ProjectService {
    @Autowired
    ProjectUserRelationService userRelationService;

    @Resource
    ProjectDao projectDao;

    @Autowired
    ScoreService scoreService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProjectEntity> page = this.page(
                new Query<ProjectEntity>().getPage(params),
                new QueryWrapper<ProjectEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void buildProject(ProjectDto projectDto) {
        //存放项目信息
        ProjectEntity projectEntity = new ProjectEntity();
        BeanUtils.copyProperties(projectDto,projectEntity);
        this.save(projectEntity);

        //存取项目人员分工信息
        Long projectId = projectEntity.getId();
        Long createdAdminId = projectDto.getCreatedAdminId();

        List<ProjectDto.Relation> userRelations = projectDto.getUserRelations();
        List<ProjectUserRelationEntity> userRelationEntities = userRelations.stream().map(relation -> {
            ProjectUserRelationEntity userRelationEntity = new ProjectUserRelationEntity();
            BeanUtils.copyProperties(relation, userRelationEntity);
            userRelationEntity.setProjectId(projectId);
            userRelationEntity.setCreatedAdminId(createdAdminId);
            return userRelationEntity;
        }).collect(Collectors.toList());
        userRelationService.saveBatch(userRelationEntities);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<ProjectAdminVo> listProject() {
        List<ProjectAdminVo> projectVoList = projectDao.listProject();
        return projectVoList;
    }

    @Override
    public List<ProjectVo> listProjectByStatus(String status, String date) {
        List<ProjectVo> projectVoList = projectDao.listProjectByStatus(status, date);
        return projectVoList;
    }

    @Override
    public List<PersonalProjectVo> listByPersonalId(String personalId) {
        List<PersonalProjectVo> personalProjectVoList = projectDao.listByPersonalId(personalId);
        return personalProjectVoList;
    }

    @Override
    public String listPersonalScore(String projectId, String userType) {
        String score = projectDao.listPersonalScore(projectId, userType);
        return score;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long projectId) {
        removeById(projectId);
        scoreService.removeByScoredId(projectId);
        userRelationService.removeByProjectId(projectId);
    }

    @Override
    public List<ProjectAdminVo> listProjectByAdmin(Long adminId) {
        List<ProjectAdminVo> projectVoList = projectDao.listProjectByAdmin(adminId);
        return projectVoList;
    }

    @Override
    public List<ProjectScoreAdminVo> listProjectByStatusAndId(String status, String date, String adminId) {
        List<ProjectScoreAdminVo> projectVoList = projectDao.listProjectByStatusAndId(status, date, adminId);
        return projectVoList;
    }

    @Override
    public void updateStatus(String projectId, String status) {
        projectDao.updateStatus(projectId, status);
    }


}